package com.genITeam.ria.bl;

import java.util.ArrayList;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.genITeam.ria.dao.PersonalMessageDAO;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.HibernateUtils;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Member;
import com.genITeam.ria.vo.PersonalMessage;
import com.genITeam.ria.vo.ResponseVo;

public class PersonalMessageBL {

	public PersonalMessageBL() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(PersonalMessageBL.class);

	/**
	 * getPersonalMessage return the search Result data in XML format.
	 * 
	 * @param member
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String getPersonalMessage(Member member) throws NGFException {
		Session session = null;
		Transaction tx = null;
		String personalMessageXML = "";
		try {
			logger.info("Start getPersonalMessage");
			System.out.println("getPersonalMessage");
			PersonalMessageDAO personalMessageDAO = new PersonalMessageDAO();

			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			ResponseVo responseVo = personalMessageDAO.getPersonalMessage(
					member, session);
			ArrayList PersonalMessageResult = responseVo
					.getPersonalMessageList();

			personalMessageXML = this
					.generatePersonalMessageXML(PersonalMessageResult);

			tx.commit();

		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

		return personalMessageXML;
	}

	/**
	 * savePersonalMessage return the search Result data in XML format.
	 * 
	 * @param member
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public void savePersonalMessage(PersonalMessage personalMessage)
			throws NGFException {

		PersonalMessageDAO personalMessageDAO = null;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start savePersonalMessage");
			personalMessageDAO = new PersonalMessageDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();

			// personalMessage.getMemberByReceiverId().get
			personalMessageDAO.savePersonalMessage(personalMessage, session);
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * savePersonalMessage Use to Get the Message Receiver name by Id 
	 * 
	 * @param String
	 * 
	 * @return int
	 * @throws NGFException
	 */
	
	public int getReceiverIdByName(String receiverName) throws NGFException {
		int receiverId = 0;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start getReceiverIdByName");
			Member member = new Member();
			PersonalMessageDAO personalMessageDAO = new PersonalMessageDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			member = personalMessageDAO.getReceiverIdByName(receiverName,
					session);
			receiverId = member.getId();
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());
		}
		return receiverId;
	}

	/**
	 * deletePersonalMessage Used to delete the Selected Personal Message
	 * 
	 * @param PersonalMessage
	 * 
	 * @return none
	 * @throws NGFException
	 */

	public void deletePersonalMessage(PersonalMessage personalMessage)
			throws NGFException {

		PersonalMessageDAO personalMessageDAO = null;
		Session session = null;
		Transaction tx = null;
		try {
			logger.info("Start deletePersonalMessage");
			personalMessageDAO = new PersonalMessageDAO();
			session = HibernateUtils.getSessionFactory().getCurrentSession();
			tx = session.getTransaction();
			tx.begin();
			personalMessageDAO.deletePersonalMessage(personalMessage, session);
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			if (tx != null && tx.isActive()) {
				tx.rollback();
			} // end of if
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

	}

	/**
	 * generateSearchForumXML Generate the XML from the collection posts found
	 * in searchForumResult ArrayLsit
	 * 
	 * @param posts
	 * 
	 * @return String
	 * @throws NGFException
	 */

	public String generatePersonalMessageXML(ArrayList PersonalMessageResult)
			throws NGFException {
		StringBuffer xml = new StringBuffer("");
		PersonalMessage personalMessage = null;
		try {
			logger.info("Start generatePersonalMessageXML");
			xml = new StringBuffer("<resultset>\n");
			for (int i = 0; i < PersonalMessageResult.size(); i++) {
				personalMessage = (PersonalMessage) PersonalMessageResult
						.get(i);

				xml.append("<result>");
				xml.append("<id>" + personalMessage.getId() + "</id>");
				xml.append("<senderName>"
						+ Utility.getStringValue(personalMessage.getMemberBySenderId().getUserName(),"")
						+ "</senderName>");
				xml.append("<receiverName>"
						+ Utility.getStringValue(personalMessage.getMemberByReceiverId().getUserName(),"")
						+ "</receiverName>");
				xml.append("<subject>" + Utility.getStringValue(personalMessage.getSubject(),"")
						+ "</subject>");
				xml.append("<message><![CDATA[" + Utility.getStringValue(personalMessage.getMessage(),"")
						+ " ]]> </message>");
				xml.append("<messageDate>" + personalMessage.getMessageDate()
						+ "</messageDate>");
				xml.append("</result>");
				xml.append("\n");

			}// end for

			xml.append("</resultset>");

			System.out.println(xml);

		} catch (Exception e) {
			System.out.println(e.toString());
			logger.error("Exception = " + e.toString());
			throw new NGFException(e.getMessage());

		}

		return xml.toString();
	}

	public static void main(String[] args) {

		try {
			PersonalMessageBL personalMessageBL = new PersonalMessageBL();
			// Member member = new Member();
			// member.setId(1);
			// personalMessageBL.getPersonalMessage(member);

			// Member memberReceiver = new Member();
			//			
			// memberReceiver.setId(1);

			personalMessageBL.getReceiverIdByName("asfgfd");

		} catch (Exception e) {

			System.out.println("Exception " + e.getMessage());
		}

	}
}
